package model

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ PmsAttrGroupModel = (*customPmsAttrGroupModel)(nil)

type (
	// PmsAttrGroupModel is an interface to be customized, add more methods here,
	// and implement the added methods in customPmsAttrGroupModel.
	PmsAttrGroupModel interface {
		pmsAttrGroupModel
		FindByCategoryIdAndPageAndKey(ctx context.Context, key string, limit, page, categoryId int64) ([]*PmsAttrGroup, error)
	}

	customPmsAttrGroupModel struct {
		*defaultPmsAttrGroupModel
	}
)

func (c *customPmsAttrGroupModel) FindByCategoryIdAndPageAndKey(ctx context.Context, key string, limit, page, categoryId int64) (res []*PmsAttrGroup, err error) {
	likeStr := "%" + key + "%"
	query := fmt.Sprintf("SELECT * FROM pms_attr_group  WHERE attr_group_id = %d and (attr_group_id = %v or attr_group_name LIKE '%s') ORDER BY sort LIMIT %d OFFSET %d",
		categoryId, key, likeStr, limit, page-1,
	)
	err = c.conn.QueryRowsCtx(ctx, &res, query)
	return res, err
}

// NewPmsAttrGroupModel returns a model for the database table.
func NewPmsAttrGroupModel(conn sqlx.SqlConn) PmsAttrGroupModel {
	return &customPmsAttrGroupModel{
		defaultPmsAttrGroupModel: newPmsAttrGroupModel(conn),
	}
}
